import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageToOledCol {

    private static int page_row = 8;
    private static int max_page = 8;
    private static int max_width = 128;
    private static int max_height = 64;

    public static void main(String[] args) {
        try {
            imgToHex("/Users/casually/Library/Mobile Documents/com~apple~CloudDocs/ps/马卡鲁黑白logo.bmp");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //printXBM("");
    }

    public static void imgToHex(String path) throws IOException {
        BufferedImage image = ImageIO.read(new File(path));
        int w = image.getWidth();
        int h = image.getHeight();

        w = w < max_width ? w : max_width;
        h = h < max_height ? h : max_height;

        int page = h / 8;

        float[] rgb = new float[3];
        double[][] zuobiao = new double[w][h];
        int R = 0;
        float red = 0;
        float green = 0;
        float blue = 0;
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int pixel = image.getRGB(x, y);
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
                red += rgb[0];
                green += rgb[1];
                blue += rgb[2];
                R = (x + 1) * (y + 1);
                float avg = (rgb[0] + rgb[1] + rgb[2]) / 3;
                zuobiao[x][y] = avg;
            }
        }
        double SW = 175;
        for (int i = 0; i < page - 1; i++) {
            int line = i * page_row;
            StringBuffer sb = new StringBuffer();
            sb.append("{");
            for (int x = 0; x < w; x++) {
                String str = "";
                for (int y = line; y < h; y++) {
                    if (y == (line + page_row)) {
                        sb.append(parseByte2HexStr(str));
                        //sb.append(str);
                        if (x != w - 1) {
                            sb.append(",");
                        }
                        break;
                    }
                    if (zuobiao[x][y] <= SW) {
                        str += "1";
                    } else {
                        str += "0";
                    }
                }
            }
            sb.append("},");
            System.out.println(sb.toString());
        }
    }

    /**
     * @param str
     * @return
     * @description 将二进制转换成16进制
     */
    public static String parseByte2HexStr(String str) {
        String hex = Integer.toHexString(Integer.parseInt(str, 2));
        return "0x" + (hex.length() == 1 ? "0" + hex : hex);
    }

    /**
     * 打印xbm
     */
    public static void printXBM(int width, int height, Integer[] hexArray) {
        for (int i = 0; i < hexArray.length; i++) {
            String b = Integer.toBinaryString(hexArray[i]);
            System.out.println(b);
        }
    }
}

